#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'
import sys
sys.path.append('.')
sys.path.append('..')
import webCapture
import config

class WebCapture(webCapture.AMFCapture):
    def __init__(self,log,x0, y0, **kwargs):
        """x0, y0我的农场坐标
        """
        webCapture.WebCapture.__init__(self,log)
        self.src_ip = config.game_ip
        #self.x0, self.y0 = x0 + 48, y0 + 307#农场中心点坐标
        self.x0, self.y0 = 48, 307#农场中心点坐标
        self.farmKind = kwargs.get('farmKind', None)
        self.farmCount = kwargs.get('farmCount', None)
        self.farmAction = kwargs.get('farmAction', 0)

        self.favor_types= self.__getFavorTypes()

    def __getFavorTypes(self):
        favor_types= []
        if self.farmKind['food']:
            favor_types.append(1) 
        if self.farmKind['wood']:
            favor_types.append(2) 
        if self.farmKind['stone']:
            favor_types.append(3) 
        if self.farmKind['bronze']:
            favor_types.append(4) 
        return favor_types

    def __parseFarm(self,response):
        def canPick(plant):
            #已成熟#剩余比较多#种类正确
            return plant['status'] == 1 and plant['curYeild'] > int(plant['standYeild']*0.6)+100 and plant['resType'] in self.favor_types

        def overLv(plant):
            """作物级别超过设置的上限
            """
            return self.farmCount['kind'] == 'lv' and plant['farmTypeId'] >= plant['resType'] * self.farmCount['value']

        def overSum(plant):
            """标准产量超过设置的上限
            """
            return self.farmCount['kind'] == 'sum' and plant['standYeild'] >= self.farmCount['value']

        if hasattr(response,'get'):
            userName = response.get('userName')
            areaList = response.get('areaList')
            plantList = response.get('plantList')
            if areaList and plantList:
                r = []
                for plant in plantList:
                    if canPick(plant):
                        if overLv(plant) or overSum(plant):
                            pos = {'posX':plant['posX'],'posY':plant['posY']}
                            if pos in areaList:
                                r.append(plant)
                if r:
                    return {'name':userName,'farms':r}
        return None

    def __getRealPos(self,x0,y0,x,y):
        ''' 得到实际坐标
        x0,y0为原点坐标, x,y为农场posX，posY
        '''
        return x0+52*(x-y),y0+29*x+26*y

    def parse(self, body):
        '''处理好友农场信息
        '''
        response = self.parseAMF3(body)
        if response:
            farm = self.__parseFarm(response)
            if farm:
                r = []
                for f in farm['farms']:
                    r.append(self.__getRealPos(self.x0,self.y0,f['posX'],f['posY']))
                self.log.info('%s:%s' %(farm['name'], str(r)))
                return {'name':farm['name'], 'points':r}

if __name__ == '__main__':
    import config
    import pickle
    log = config.log
    f = open('user_file.txt', 'r')
    data = f.read()
    f.close()
    data = pickle.loads(data)
    print data

    capture = WebCapture(log,610, 43,  **data)
    pc = capture.getCapture()
    #pc = capture.getFixedCapture()

    #pc = capture.getFileCapture(r'qq7x/data_home.pcap')
    #pc = capture.getFileCapture(r'qq7x/data_office.pcap')
    if pc:
        record=capture.process(pc)
        for crop in record['points']:
            print crop
        
